﻿Imports System.Xml
Imports System.IO

Public Class XMLSoftWareBigType

    Private Shared _filePath As String = Util.GetSoftWareBigTypeXml
    Private Shared _xmldoc As XmlDocument

    Public Sub New()
        If _xmldoc Is Nothing Then
            _xmldoc = New XmlDocument
            If Not File.Exists(_filePath) Then
                Util.CreateBigTypeTemple()
            End If
            _xmldoc.Load(_filePath)
        End If
    End Sub

    Public Function Create(ByVal ty As CSoftWareBigType) As Boolean

        Dim dwt As XmlElement = _xmldoc.CreateElement("SoftWareBigType")
        Dim idnode As XmlElement = _xmldoc.CreateElement("Id")
        idnode.InnerText = ty.Id
        dwt.AppendChild(idnode)

        Dim namenode As XmlElement = _xmldoc.CreateElement("Name")
        namenode.InnerText = ty.Name
        dwt.AppendChild(namenode)

        Dim iconpath As XmlElement = _xmldoc.CreateElement("IconPath")
        iconpath.InnerText = ty.IconPath
        dwt.AppendChild(iconpath)

        Dim iconpathtype As XmlElement = _xmldoc.CreateElement("IconpathType")
        iconpathtype.InnerText = ty.IconpathType
        dwt.AppendChild(iconpathtype)

        _xmldoc.DocumentElement.AppendChild(dwt)

        _xmldoc.Save(_filePath)

        Return True

    End Function

    Public Function Update(ByVal ty As CSoftWareBigType) As Boolean

        Dim selectNode As XmlNode = _xmldoc.DocumentElement.SelectSingleNode("//SoftWareBigType[Id='" + ty.Id + "']")
        If selectNode IsNot Nothing Then
            selectNode.SelectSingleNode("./Id").InnerText = ty.Id
            selectNode.SelectSingleNode("./Name").InnerText = ty.Name
            selectNode.SelectSingleNode("./IconPath").InnerText = ty.IconPath
            selectNode.SelectSingleNode("./IconpathType").InnerText = ty.IconpathType
        End If

        _xmldoc.Save(_filePath)

        Return True

    End Function

    Public Function Delete(ByVal id As String) As Boolean

        Dim selectNode As XmlNode = _xmldoc.DocumentElement.SelectSingleNode("//SoftWareBigType[Id='" + id + "']")
        If selectNode IsNot Nothing Then
            _xmldoc.DocumentElement.RemoveChild(selectNode)
        End If

        _xmldoc.Save(_filePath)

        Return True

    End Function

    Public Function GetById(ByVal id As String) As CSoftWareBigType

        Dim ty As New CSoftWareBigType
        Dim selectNode As XmlNode = _xmldoc.DocumentElement.SelectSingleNode("//SoftWareBigType[Id='" + id + "']")
        If selectNode IsNot Nothing Then
            ty.Id = selectNode.SelectSingleNode("./Id").InnerText
            ty.Name = selectNode.SelectSingleNode("./Name").InnerText
            ty.IconPath = selectNode.SelectSingleNode("./IconPath").InnerText
            ty.IconpathType = selectNode.SelectSingleNode("./IconpathType").InnerText
        End If

        Return ty

    End Function

    Public Function GetAll() As List(Of CSoftWareBigType)

        Dim tys As New List(Of CSoftWareBigType)
        Dim selectnodes As XmlNodeList = _xmldoc.DocumentElement.SelectNodes("//SoftWareBigType")
        For index As Integer = 0 To selectnodes.Count - 1
            Dim selectNode As XmlNode = selectnodes(index)
            Dim ty As New CSoftWareBigType
            ty.Id = selectNode.SelectSingleNode("./Id").InnerText
            ty.Name = selectNode.SelectSingleNode("./Name").InnerText
            ty.IconPath = selectNode.SelectSingleNode("./IconPath").InnerText
            ty.IconpathType = selectNode.SelectSingleNode("./IconpathType").InnerText
            tys.Add(ty)
        Next

        Return tys

    End Function

    Public Function GetMaxid() As String

        Dim selectnodes As XmlNodeList = _xmldoc.DocumentElement.SelectNodes("//Id")
        Dim maxid As Integer = 0
        For index As Integer = 0 To selectnodes.Count - 1
            If CInt(selectnodes(index).InnerText) > maxid Then
                maxid = CInt(selectnodes(index).InnerText)
            End If
        Next

        Return CStr(maxid + 1).PadLeft(4, "0"(0))


    End Function

End Class
